package com.unicampmaps;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Layout used to paint the balloon on the map. Requires layout/balloon.xml.
 * 
 * @author isaac.salgueiro@discalis.com (based on Jeff Gilfelt's
 *         BalloonItemizedOverlay)
 */
public class BalloonLayout extends FrameLayout {

        private final LinearLayout layout;
        private final TextView title;
        private final TextView snippet;
        private final ImageView closeButton;

        public BalloonLayout(Context context, int balloonBottomOffset) {
                super(context);
                
                setPadding(10, 0, 10, balloonBottomOffset);
                layout = new LinearLayout(context);
                layout.setVisibility(VISIBLE);
                // FIXME reuse the view
                final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                
                View tempView = inflater.inflate(R.layout.balloon_overlay, layout);
                
                title = (TextView) tempView.findViewById(R.id.balloon_item_title);
                snippet = (TextView) tempView.findViewById(R.id.balloon_item_snippet);
                closeButton = (ImageView) tempView.findViewById(R.id.balloon_close);
                
                closeButton.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                            if (arg1.getAction() == MotionEvent.ACTION_UP) {
                            	BalloonLayout.this.setVisibility(View.GONE);
                            }
                            return true;
                    }
                });

                final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		        params.gravity = Gravity.NO_GRAVITY;
		        addView(layout, params);
        }

        /**
         * Sets the text shown into the balloon.
         * 
         * @param text
         */
        public void setText(String text, String snippetText) {
                layout.setVisibility(VISIBLE);
                if (text != null) {
                    title.setVisibility(VISIBLE);
                    title.setText(text);
                    
                    snippet.setVisibility(VISIBLE);
                    snippet.setText(snippetText);
                } else {
                    title.setVisibility(GONE);
                    snippet.setVisibility(GONE);
                }
        }

}